﻿using System;
using System.Windows;
using DSPL.Services;
using DSPL.Contracts;
using DSPL.Common;
using System.Data;
using DSPL.Services.Services;
using DSPL.Contracts.Common;
using DSPL.Contracts.ViewModel;
using DSPL.Contracts.Command;
using DSPL.Finac.Common;
using System.Windows.Input;

namespace DSPL.Finac.ViewModel
{
    public class PriceLevelMasterViewModel : BaseViewModel
    {
        private bool isPriceCodeFocused;
        private bool isPriceNameFocused;
        private bool isCurrencyCodeFocused;
        private bool isCurrencyCodeEnabled;
        private int  _PriceLevelStatus;

        #region Define properties

        public bool IsPriceCodeFocused
        {
            get { return isPriceCodeFocused; }
            set
            {
                isPriceCodeFocused = value;
                this.OnPropertyChanged("IsPriceCodeFocused");

            }
        }

         public bool IsPriceNameFocused
        {
            get { return isPriceNameFocused; }
            set
            {
                isPriceNameFocused = value;
                this.OnPropertyChanged("IsPriceNameFocused");

            }
        }
                
        public bool IsCurrencyCodeFocused
        {
            get { return isCurrencyCodeFocused; }
            set
            {
                isCurrencyCodeFocused = value;
                this.OnPropertyChanged("IsCurrencyCodeFocused");
            }
        }

        public bool IsCurrencyCodeEnabled
        {
            get { return isCurrencyCodeEnabled; }
            set
            {
                isCurrencyCodeEnabled = value;
                this.OnPropertyChanged("IsCurrencyCodeEnabled");
            }
        }    

        #endregion


        #region Constructor

        public PriceLevelMasterViewModel()
            : base()
        {
            MessageTitle = "Price Level Master";
        }

        #endregion
        /// <summary>
        /// Validate mandatory data before save
        /// </summary>
        /// <param name="obj"></param>
        protected override void PreDataValidateExecuted(object obj)
        {
            IsPriceCodeFocused = false;
            IsPriceNameFocused = false;
            IsCurrencyCodeFocused = false;

            var messageService = new MessageService();
            if (string.IsNullOrEmpty(CurrentRow["PrilCode"].ToString().Trim()))
            {
                messageService.ShowMessage(MessageText.PriceLevelNotEmpty, MessageTitle, MessageType.InfoOK);
                IsPriceCodeFocused = true;
                this.IsValid = false;
                return;
            }

            if (string.IsNullOrEmpty(CurrentRow["PrilName"].ToString().Trim()))
            {
                messageService.ShowMessage(MessageText.PriceLevelNameNotEmpty, MessageTitle, MessageType.InfoOK);
                IsPriceNameFocused = true;
                this.IsValid = false;
                return;
            }

            if (Convert.ToInt32(CurrentRow["CurPk"])==0)
            {
                messageService.ShowMessage(MessageText.CurrencyCodeNotEmpty, MessageTitle, MessageType.InfoOK);
                IsCurrencyCodeFocused = true;
                this.IsValid = false;
                return;
            }
             
            this.IsValid = true;
        }

        protected override void RefreshExternalDataObjectsExecueted(object obj)
        {

            base.RefreshExternalDataObjectsExecueted(obj);

            if (FormMode == Modes.New)
            {
                CurrentRow["Active"] = 1;

                int baseCurrencyPk = (int)(new MasterReadService().GetCurrencyKey(GlobalVariables.BaseCurrency));

                if (GlobalVariables.SystemCurrency.ToUpper() == "LOCAL")
                {
                    CurrentRow["CurPk"] = baseCurrencyPk;

                }
                else
                {
                    if (_PriceLevelStatus == 0)
                    {
                        CurrentRow["CurPk"] = baseCurrencyPk;
                    }
                }
                OnPropertyChanged("CurrentRow");

            }
        }

        protected override void EnableDisableMasterSpecificControls()
        {
            base.EnableDisableMasterSpecificControls();
            
            if (GlobalVariables.SystemCurrency.ToUpper() == "LOCAL")
            {
                IsCurrencyCodeEnabled = false;
            }
            else
            {
                var dtPriceStatus = new MasterReadService().GetPriceLevelStatus();
                 _PriceLevelStatus = Convert.ToInt32(dtPriceStatus.Rows[0]["Status"]);

                IsCurrencyCodeEnabled = (_PriceLevelStatus == 0) ? false : true;

            }
           

        }

    }
}